﻿using NCP.Infrastructure.Common.Specification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NCP.Recruiting.Domain.Aggregates.Resumes
{
    public class ResumeSpecifications
    {
        public static Specification<Resume> GetByWorkDescription(String description)
        {
            if (!String.IsNullOrWhiteSpace(description))
            {
                return new DirectSpecification<Resume>(x => x.Works.Any(y => y.Description.Contains(description)));
            }
            else
            {
                return new DirectSpecification<Resume>(x => true);
            }
        }

        public static Specification<Resume> GetByAgeRange(int? minAge, int? maxAge)
        {
            var currentYear = DateTime.Now.Year;
            var minAgeSpecification = new DirectSpecification<Resume>(x => true);
            if (minAge.HasValue)
            {
                var maxDate = DateTime.Now.Date.AddYears(-minAge.Value);
                minAgeSpecification = new DirectSpecification<Resume>(x => x.BirthDate.HasValue && x.BirthDate.Value <= maxDate);
            }

            var maxAgeSpecification = new DirectSpecification<Resume>(x => true);
            if (maxAge.HasValue)
            {
                var minDate = DateTime.Now.Date.AddYears(-maxAge.Value - 1);
                minAgeSpecification = new DirectSpecification<Resume>(x => x.BirthDate.HasValue && x.BirthDate.Value > minDate);
            }
            return minAgeSpecification && maxAgeSpecification;
        }

        public static Specification<Resume> GetByYearsOfExperience(int? yearsOfExperience)
        {
            return new DirectSpecification<Resume>(x => !yearsOfExperience.HasValue || (x.YearsOfExperience.HasValue && x.YearsOfExperience.Value == yearsOfExperience));
        }

        public static Specification<Resume> GetByMajor(int? major)
        {
            return new DirectSpecification<Resume>(x => !major.HasValue || (x.Major.HasValue && x.YearsOfExperience.Value == major));
        }

        public static Specification<Resume> GetByDesiredIndustry(int? desiredIndustry)
        {
            return new DirectSpecification<Resume>(x => !desiredIndustry.HasValue || (x.DesiredIndustry.HasValue && x.DesiredIndustry.Value == desiredIndustry));
        }

        public static Specification<Resume> GetByEducationLevel(int? educationLevel)
        {
            return new DirectSpecification<Resume>(x => !educationLevel.HasValue || (x.EducationLevel.HasValue && x.EducationLevel.Value == educationLevel));
        }

        public static Specification<Resume> GetByLastUpdate(int? days)
        {
            if (days.HasValue)
            {
                var minLastUpdated = DateTime.Now.Date.AddDays(-days.Value);
                return new DirectSpecification<Resume>(x => x.LastUpdated >= minLastUpdated);
            }
            else
            {
                return new DirectSpecification<Resume>(x => true);
            }
        }

        public static Specification<Resume> GetByReferenceNumber(String referenceNumber)
        {
            if (!String.IsNullOrWhiteSpace(referenceNumber))
            {
                return new DirectSpecification<Resume>(x => x.ReferenceNumber.Contains(referenceNumber));
            }
            else
            {
                return new DirectSpecification<Resume>(x => true);
            }
        }

        public static Specification<Resume> GetByJobTypeList(IEnumerable<int> jobTypeList)
        {
            if (jobTypeList != null && jobTypeList.Count() > 0)
            {
                return new DirectSpecification<Resume>(x => x.DesiredJobType.HasValue && jobTypeList.Contains(x.DesiredJobType.Value));
            }
            else
            {
                return new DirectSpecification<Resume>(x => true);
            }
        }
    }
}
